/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.easyidea.its.domain.model.system;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.easyidea.its.domain.model.BaseEntity;

/**
 * 
 * @author hukn
 */
@Entity
@Table(name = "User")
public class User extends BaseEntity {
	private static final long serialVersionUID = 1L;

	private String password;

	private String userName;

	private String name;

	private Integer status;

	private Role role;

	private Department department;

	private List<PassPort> passPortList;

	public User() {
	}

	@Column(name = "password")
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "user_name")
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Column(name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "status")
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@JoinColumn(name = "role_id", referencedColumnName = "id")
	@ManyToOne(fetch = FetchType.LAZY)
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	@JoinColumn(name = "department_id", referencedColumnName = "id")
	@ManyToOne(fetch = FetchType.LAZY)
	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "UserAndPassPort", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "passport_id", referencedColumnName = "id"))
	public List<PassPort> getPassPortList() {
		return passPortList;
	}

	public void setPassPortList(List<PassPort> passPortList) {
		this.passPortList = passPortList;
	}

	/**
	 * get permission url list
	 * 
	 * @return
	 */
	@Transient
	public List<Integer> getAllPermissionIdList() {
		List<Integer> userPermissionIdList = new ArrayList<Integer>(0);
		if (this.role != null) {
			List<Permission> permissionList = this.role.getPermissionList();
			for (Permission permission : permissionList) {
				userPermissionIdList.add(permission.getId());
			}
		}
		return userPermissionIdList;
	}

	/**
	 * get permission url list
	 * 
	 * @return
	 */
	@Transient
	public List<String> getAllPermissionUrlList() {
		List<String> userPermissionUrlList = new ArrayList<String>(0);
		if (this.role != null) {
			List<Permission> permissionList = this.role.getPermissionList();
			for (Permission permission : permissionList) {
				List<PermissionAction> permissionActionList = permission
						.getPermissionActionList();
				for (PermissionAction permissionAction : permissionActionList) {
					userPermissionUrlList
							.add(permissionAction.getPackageName());
				}
			}
		}
		return userPermissionUrlList;
	}
}
